package servlets;

import interpreters.*;
import definitions.*;
import java.util.*;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class MediConflictServlet extends HttpServlet{
  	private static final long serialVersionUID = 1L;

	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/xml");
        resp.getWriter().println("<response>");
        try{
        	System.out.println(LoginServlet.getUserNames(req.getParameter("loginName"), req.getParameter("password")) + " | " + req.getParameter("userName"));
        	if(!LoginServlet.getUserNames(req.getParameter("loginName"), req.getParameter("password")).contains(req.getParameter("userName"))) throw new Exception("Not authenticated.");
        	resp.getWriter().println(returnCurrentInfo(req));
        }
        catch(Exception e){
        	StackTraceElement[] stack = e.getStackTrace();
        	resp.getWriter().println(e.getLocalizedMessage() + "<br/>");
        	for(StackTraceElement s:stack){
        		resp.getWriter().println(s.toString() + "<br/>");
        	}
        	
        }
        resp.getWriter().println("</response>");
    }
	
	private String returnCurrentInfo(HttpServletRequest req){
		String response = 
		"<currentinfo>" + 
			getDosingBlock(req) +
			getConflictsBlock(req) +
			getRemindersBlock(req) +
		"</currentinfo>";
		
		return response;
	}
	
	private static String getDosingBlock(HttpServletRequest req){
		LinkedList<LinkedList<String>> dosages = DetailedScheduleGenerator.getEvents(req.getParameter("userName"));
		String dosingInfo = "<dosings>";
		for(int dayOfWeek = 0; dayOfWeek < 7; dayOfWeek++){
			dosingInfo = dosingInfo + "<daylist name=\"" + DetailedScheduleGenerator.dayStrings[dayOfWeek] + "\">"; 
			List<String> dailyDoseList = dosages.get(dayOfWeek);
			for(String dose:dailyDoseList){
				String[] doseInfo = dose.split("-");
				dosingInfo = dosingInfo + "<dosage>" +
					"<medicine>" + doseInfo[0] + "</medicine>" +
					"<time>" +
						"<day>" + DetailedScheduleGenerator.dayStrings[Integer.parseInt(doseInfo[1].trim())] + "</day>" +
						"<hour>" + Integer.parseInt(doseInfo[2].trim())/60 + "</hour>" +
						"<minute>" + Integer.parseInt(doseInfo[2].trim())%60 + "</minute>" +
					"</time>" +
					"<amount>" + doseInfo[3] + "</amount>" +
					"<unit>" + doseInfo[4] + "</unit>" +
				"</dosage>";
			}
			dosingInfo = dosingInfo + "</daylist>";
		}
		return dosingInfo + "</dosings>";
	}
	
//	private static String getDosingBlock(HttpServletRequest req){
//		LinkedList<LinkedList<String>> dosages = DetailedScheduleGenerator.getEvents(req.getParameter("userName"));
//		String dosingInfo = "<dosings>";
//		for(int dayOfWeek = 0; dayOfWeek < 7; dayOfWeek++){
//			dosingInfo = dosingInfo + "<daylist name=\"" + DetailedScheduleGenerator.dayStrings[dayOfWeek] + "\">"; 
//			List<String> dailyDoseList = dosages.get(dayOfWeek);
//			for(String dose:dailyDoseList){
//				String[] doseInfo = dose.split("-");
//				dosingInfo = dosingInfo + "<p>" +
//					doseInfo[3] + " " + doseInfo[4] + " of " + doseInfo[0] + " @ " + parseTime(doseInfo[2]) +
//				"</p>";
//			}
//			dosingInfo = dosingInfo + "</daylist>";
//		}
//		return dosingInfo + "</dosings>";
//	}
//	
//	private static String parseTime(String t){
//		t = t.trim();
//		int time = Integer.parseInt(t);
//		int hr = time / 60;
//		String AM_PM = (hr < 12 ? "AM" : "PM");
//		hr %= 12;
//		String min = "" + time % 60;
//		if(min.length() == 1) min = "0" + min;
//		return "" + hr + ":" + min + " " + AM_PM;
//	}
	
	private static String getConflictsBlock(HttpServletRequest req){
		String conflictInfo = "<conflicts>";
	
		List<Conflict> conflicts = ConflictChecker.getConflicts(req.getParameter("userName"));
		if(conflicts == null) return "";
		
		for(Conflict c:conflicts){
			conflictInfo = conflictInfo + "<conflict>" +
				"<item1>" + c.item1 + "</item1>" +
				"<item2>" + c.item2 + "</item2>" +
			"</conflict>";
		}
		
		return conflictInfo + "</conflicts>";
	}
	
	private static String getRemindersBlock(HttpServletRequest req){	
		String reminderInfo = "<reminders>";
		
		List<String> reminders = ReminderChecker.getReminders(req.getParameter("userName"));
		if(reminders == null) return "";
		for(String s:reminders){
			String[] remindata = s.split("-");
			reminderInfo = reminderInfo + "<reminder>";
				reminderInfo = reminderInfo + "<medicine>";
				reminderInfo = reminderInfo + remindata[0];
				reminderInfo = reminderInfo + "</medicine>";
				reminderInfo = reminderInfo + "<time>";
				reminderInfo = reminderInfo + remindata[1];
				reminderInfo = reminderInfo + "</time>";
				reminderInfo = reminderInfo + "<day>";
				reminderInfo = reminderInfo + remindata[2];
				reminderInfo = reminderInfo + "</day>";
			reminderInfo = reminderInfo + "</reminder>";
		}
		
		return reminderInfo + "</reminders>";
	}
}